Imports ccapps.persistent.CaseTracker
Public Class CaseReports
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblContextHeader As System.Web.UI.WebControls.Label
    Protected WithEvents btnSearch As System.Web.UI.WebControls.Button
    Protected WithEvents ddlStatus As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents ddlType As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtItemID As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtArea As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtReason As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblCaseItemID As System.Web.UI.WebControls.Label
    Protected WithEvents DatePicker1 As Library.WebControls.DatePicker
    Protected WithEvents DatePicker2 As Library.WebControls.DatePicker

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PageTitle = "Case Reports"

        If Not persistent.CaseTracker.IsUserAuthorized(Session("user"), AuthorizationLevel.ViewAndCreate) Then
            Page.Controls.Clear()
            Response.Write("You are not authorized to view this page!")
            Return
        End If

        If Not Me.IsPostBack Then
            For Each itemTypeName As String In ItemType.GetNames(GetType(ItemType))
                'Me.ddlType.Items.Add(New ListItem(itemTypeName, [Enum].Parse(GetType(ItemType), itemTypeName)))
                Me.ddlType.Items.Add(itemTypeName)
            Next

            For Each item As ListItem In persistent.CaseTracker.GetListItemsFromEnum(GetType(persistent.CaseTracker.CaseStates))
                Me.ddlStatus.Items.Add(item)
            Next

            Me.ddlType.Items.Insert(0, New ListItem("All", ""))
            Me.ddlType.SelectedIndex = 0

            Dim queryHolder As String = String.Empty
            Me.DataGrid1.DataSource = persistent.CaseTracker.GetCases(queryHolder, Session("user"), 10)
            Me.DataGrid1.DataBind()
            Me.PersistedQuery = queryHolder
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim queryHolder As String = String.Empty
        Dim startDate As DateTime = Me.DatePicker1.DateValue
        Dim endDate As DateTime = Me.DatePicker2.DateValue.AddSeconds(86399)

        If endDate.AddSeconds(-86399) = DateTime.MinValue Then
            endDate = DateTime.Parse("31.12.2029")
        End If

        Me.DataGrid1.CurrentPageIndex = 0
        Me.DataGrid1.DataSource = persistent.CaseTracker.FindCases(Me.ddlType.SelectedValue, startDate, endDate, Me.txtItemID.Text, Me.txtArea.Text, Me.ddlStatus.SelectedValue, Me.txtReason.Text, queryHolder, Session("user"))
        Me.DataGrid1.DataBind()
        Me.PersistedQuery = queryHolder
    End Sub

    Private Property PersistedQuery() As String
        Get
            If Not viewstate("_PersistedQuery") Is Nothing Then
                Return viewstate("_PersistedQuery").ToString()
            Else
                Return String.Empty
            End If
        End Get
        Set(ByVal Value As String)
            viewstate("_PersistedQuery") = Value
        End Set
    End Property

    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        Me.DataGrid1.CurrentPageIndex = e.NewPageIndex
        Me.DataGrid1.DataSource = persistent.myDBConn.myORATable(Me.PersistedQuery)
        Me.DataGrid1.DataBind()
    End Sub
End Class
